package com.cy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class NacosConfigApplication {

    private static Logger logger = LoggerFactory.getLogger(NacosConfigApplication.class);
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class,args);
        logger.debug("---debug---");
        logger.info("---info---");
        logger.error("---error---");

    }

    @RefreshScope   //动态获取@value获取的值,基于此类中url访问时会重新构建对象，初始化logLevel
    @RestController
    public class  NacosConfigController {

        @Value("${logging.level.com.cy:error}")
        private String logLevel;

        @Value("${server.tomcat.threads.max:200}")
        private String maxThread;

        @Value("${page.pagesize:5}")
        private Integer size;

        //测试共享配置
        @GetMapping("/config/doGetPageSize")
        public String doGetPageSize() {

            return "page.pagesize=" + size;

        }

        //测试分组
        @GetMapping("/config/doGetMaxThread")
        public String doGetMaxThread() {

            return "thread.max=" + maxThread;

        }

        @GetMapping("/config/log/level")
        public String doPrintLogLevel() {

            System.out.println("log.level=" + logLevel);
            //修改nacos配置之后能够动态获取logLevle
            logger.info("---info---");
            logger.debug("---debug---");
            logger.error("---error---");
            return "log level is:" + logLevel;

        }

    }

}
